package com.easyidea.its.web.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.easyidea.its.util.FTPClientUtil;
import com.easyidea.its.util.FTPConfig;


public class ShowImageServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		String firstPicPath = req.getParameter("firstPicPath");
		
//		SeesionUserDto dto = (SeesionUserDto) req.getSession().getAttribute(
//				ItsConstants.SESSION_KEY_USER_INFO);
//		TrackLog trackLog = (TrackLog) req.getSession().getAttribute(
//				dto.getUserId() + "." + "tracklog");
		if (firstPicPath != null) {
			FTPConfig config = FTPClientUtil.buildFtpConfig(firstPicPath, null, null, null, null);

			try{
				InputStream hFile = FTPClientUtil.downLoadFtpFile(config);

				res.setContentType("image/*"); // 设置返回的文件类型
				OutputStream toClient = res.getOutputStream(); // 得到向客户端输出二进制数据的对象
				byte[] bytes = new byte[4096];
				int c;
				while ((c = hFile.read(bytes)) != -1) {
					toClient.write(bytes, 0, c);
				}
				toClient.write(bytes); // 输出数据
				toClient.close();
//				double maxLimit = 400;
//				double ratio = 1.0;	
//				BufferedImage Bi = ImageIO.read(hFile);
//				if ((Bi.getHeight() > maxLimit) || (Bi.getWidth() > maxLimit)) {
//				if (Bi.getHeight() > Bi.getWidth())
//				ratio = maxLimit / Bi.getHeight();
//				else
//				ratio = maxLimit / Bi.getWidth();
//				}
//				int widthdist=(int)Math.floor(Bi.getWidth() * ratio), 
//				       heightdist =(int)Math.floor(Bi.getHeight() * ratio);
//
//				BufferedImage tag = new BufferedImage(widthdist, heightdist,
//				BufferedImage.TYPE_INT_RGB);
//
//				tag.getGraphics().drawImage(Bi.getScaledInstance(widthdist, heightdist, BufferedImage.SCALE_SMOOTH), 0, 0, null);
//
//				ImageIO.write(tag, "JPEG", res.getOutputStream());				
			} catch (Exception e) // 错误处理
			{
				PrintWriter toClient = res.getWriter(); // 得到向客户端输出文本的对象
				res.setContentType("text/html;charset=gbk");
				toClient.write("无法打开图片!");
				toClient.close();
			}
		}
	}

	
}